Added xen-script-common.sh, for functions common to all scripts, not just the
authoremellor@leeni.uk.xensource.com <emellor@leeni.uk.xensource.com>
Sun, 30 Oct 2005 12:33:17 +0000 (13:33 +0100)
committeremellor@leeni.uk.xensource.com <emellor@leeni.uk.xensource.com>
Sun, 30 Oct 2005 12:33:17 +0000 (13:33 +0100)
hotplug ones.  Added evalVariables and findCommand functions to that, which we
use to clarify the handling of variables being passed in as command line
variables.

Make vif-bridge able to find the bridge for itself if only one bridge is in
use.  This means that it is not necessary to specify a bridge in many
configurations.  Allow the bridge to be specified on the command line, meaning
that a default may be provided in the xend-config.sxp if desired.

Added xenstore_read_default to xen-hotplug-common.sh, which reads from the
store but uses a given default if the path in the store is not present.  This
is used by vif-bridge to allow the store details (i.e. those given to xm create)
to override the default value given in the xend-config.sxp.

Remove vif-bridge setting -- the value can be specified on the vif-script
command line if necessary.

Added examples for network-nat/vif-nat.

Added lots of big comments.

Signed-off-by: Ewan Mellor <ewan@xensource.com>
tools/examples/Makefile
tools/examples/network-bridge
tools/examples/vif-bridge
tools/examples/vif-common.sh
tools/examples/xen-hotplug-common.sh
tools/examples/xen-script-common.sh [new file with mode: 0644]
tools/examples/xend-config.sxp

index 54164235b499c8fc5d99e5b68e6b03734252509a..ea358adabf1276d9a445f90aa0461dae74182964 100644 (file)
@@ -25,6 +25,7 @@ XEN_SCRIPTS += network-route vif-route
 XEN_SCRIPTS += network-nat vif-nat
 XEN_SCRIPTS += block
 XEN_SCRIPTS += block-enbd block-nbd
+XEN_SCRIPTS += xen-script-common.sh
 XEN_SCRIPTS += xen-hotplug-common.sh xen-network-common.sh vif-common.sh
 XEN_SCRIPTS += block-common.sh
 
index e9a75ea749eae053203e62e421ae47b7ef952abf..4330c3332ebd13b0bfab5077ddd053b8048dc233 100755 (executable)
 #
 #============================================================================
 
+
 dir=$(dirname "$0")
+. "$dir/xen-script-common.sh"
 . "$dir/xen-network-common.sh"
 
-# Exit if anything goes wrong.
-set -e 
-
-# First arg is the operation.
-OP=$1
-shift
-
-# Pull variables in args in to environment.
-for arg ; do export "${arg}" ; done
+findCommand "$@"
+evalVariables "$@"
 
 vifnum=${vifnum:-0}
 bridge=${bridge:-xenbr${vifnum}}
@@ -289,7 +284,7 @@ op_stop () {
     brctl delbr ${bridge}
 }
 
-case ${OP} in
+case "$command" in
     start)
        op_start
        ;;
@@ -303,7 +298,7 @@ case ${OP} in
        ;;
 
     *)
-       echo 'Unknown command: ' ${OP} >&2
+       echo "Unknown command: $command" >&2
        echo 'Valid commands are: start, stop, status' >&2
        exit 1
 esac
index 619762fe302ce1a42d375051aeab048ff377aa8b..4ba683c25caf7666cfefdb7e476d144a317784cc 100755 (executable)
@@ -16,7 +16,8 @@
 # XENBUS_PATH path to this device's details in the XenStore (required).
 #
 # Read from the store:
-# bridge  bridge to add the vif to (required).
+# bridge  bridge to add the vif to (optional).  Defaults to searching for the
+#         bridge itself.
 # ip      list of IP networks for the vif, space-separated (optional).
 #
 # up:
 dir=$(dirname "$0")
 . "$dir/vif-common.sh"
 
-bridge=$(xenstore_read "$XENBUS_PATH/bridge")
+bridge=${bridge:-}
+bridge=$(xenstore_read_default "$XENBUS_PATH/bridge" "$bridge")
+
+if [ -z "$bridge" ]
+then
+  bridge=$(brctl show | cut -d "
+" -f 2 | cut -f 1)
+
+  if [ -z "$bridge" ]
+  then
+     fatal "Could not find bridge, and none was specified"
+  fi
+fi
 
 case "$command" in
     up)
@@ -54,4 +67,4 @@ esac
 
 handle_iptable
 
-log debug "vif-bridge operation for $vif successful."
+log debug "Successful vif-bridge operation for $vif, bridge $bridge."
index 48e3cf9bc7e591d1e6e5f82daa50bbe21e47cbcc..0f8fcc706f99c4671ba177c77f4606e766a498ed 100644 (file)
@@ -20,7 +20,7 @@ dir=$(dirname "$0")
 . "$dir/xen-hotplug-common.sh"
 . "$dir/xen-network-common.sh"
 
-command="$1"
+findCommand "$@"
 
 if [ "$command" != "up" ] && [ "$command" != "down" ]
 then
@@ -29,11 +29,20 @@ then
 fi
 
 
+# Parameters may be read from the environment, the command line arguments, and
+# the store, with overriding in that order.  The environment is given by the
+# driver, the command line is given by the Xend global configuration, and
+# store details are given by the per-domain or per-device configuration.
+
+evalVariables "$@"
+
+ip=${ip:-}
+ip=$(xenstore_read_default "$XENBUS_PATH/ip" "$ip")
+
+# Check presence of compulsory args.
 XENBUS_PATH="${XENBUS_PATH:?}"
 vif="${vif:?}"
 
-ip=$(xenstore-read "$XENBUS_PATH/ip" >&/dev/null || true)
-
 
 function frob_iptable()
 {
index 5beb12d60a92fa9602dc9c6bad9558140cd39980..cc5af864a28af9fcbee598264063394ca4194d50 100644 (file)
@@ -16,7 +16,8 @@
 #
 
 
-set -e
+dir=$(dirname "$0")
+. "$dir/xen-script-common.sh"
 
 export PATH="/sbin:/bin:/usr/bin:/usr/sbin:$PATH"
 export LANG="POSIX"
@@ -33,15 +34,39 @@ fatal() {
   exit 1
 }
 
+##
+# xenstore_read <path>+
+#
+# Read each of the given paths, returning each result on a separate line, or
+# exit this script if any of the paths is missing.
+#
 xenstore_read() {
   local v=$(xenstore-read "$@" || true)
   [ "$v" != "" ] || fatal "xenstore-read $@ failed."
   echo "$v"
 }
 
+
+##
+# xenstore_read_default <path> <default>
+#
+# Read the given path, returning the value there or the given default if the
+# path is not present.
+#
+xenstore_read_default() {
+  xenstore-read "$1" || echo "$2"
+}
+
+
+##
+# xenstore_write (<path> <value>)+
+#
+# Write each of the key/value pairs to the store, and exit this script if any
+# such writing fails.
+#
 xenstore_write() {
   log debug "Writing $@ to xenstore."
-  xenstore-write "$@" || log err "Writing $@ to xenstore failed."
+  xenstore-write "$@" || fatal "Writing $@ to xenstore failed."
 }
 
 log debug "$@" "XENBUS_PATH=$XENBUS_PATH"
diff --git a/tools/examples/xen-script-common.sh b/tools/examples/xen-script-common.sh
new file mode 100644 (file)
index 0000000..f6841ac
--- /dev/null
@@ -0,0 +1,44 @@
+#
+# Copyright (c) 2005 XenSource Ltd.
+#
+# This library is free software; you can redistribute it and/or
+# modify it under the terms of version 2.1 of the GNU Lesser General Public
+# License as published by the Free Software Foundation.
+#
+# This library is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+# Lesser General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public
+# License along with this library; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+#
+
+
+set -e
+
+
+evalVariables()
+{
+  for arg in "$@"
+  do
+    if expr 'index' "$arg" '=' '>' '1' >/dev/null
+    then
+      eval "$arg"
+    fi
+  done
+}
+
+
+findCommand()
+{
+  for arg in "$@"
+  do
+    if ! expr 'index' "$arg" '=' >/dev/null
+    then
+      command="$arg"
+      return
+    fi
+  done
+}
index ba433cd7450bae7bdcaa5e9d3ea1bf582bea9306..13c38de24e8ec29d279c9c1c4e4f69f64a48a98c 100644 (file)
 # The limit (in kilobytes) on the size of the console buffer
 #(console-limit 1024)
 
-## Use the following if VIF traffic is routed.
-# The script used to start/stop networking for xend.
-#(network-script     network-route)
-# The default script used to control virtual interfaces.
-#(vif-script         vif-route)
-
-## Use the following if VIF traffic is bridged.
-# The script used to start/stop networking for xend.
-(network-script    network-bridge)
-# The default bridge that virtual interfaces should be connected to.
-(vif-bridge        xenbr0)
-# The default script used to control virtual interfaces.
-(vif-script        vif-bridge)
+##
+# To bridge network traffic, like this:
+#
+# dom0: fake eth0 -> vif0.0 -+
+#                            |
+#                          bridge -> real eth0 -> the network
+#                            |
+# domU: fake eth0 -> vifN.0 -+
+#
+# use
+#
+# (network-script network-bridge)
+#
+# Your eth0 is used as the outgoing interface, by default.  To use a different
+# one (e.g. eth1) use
+#
+# (network-script 'network-bridge netdev=eth1')
+#
+# The bridge is named xenbr0, by default.  To rename the bridge, use
+#
+# (network-script 'network-bridge bridge=<name>')
+#
+# It is possible to use the network-bridge script in more complicated
+# scenarios, such as having two outgoing interfaces, with two bridges, and
+# two fake interfaces per guest domain.  To do things like this, write
+# yourself a wrapper script, and call network-bridge from it, as appropriate.
+#
+(network-script network-bridge)
+
+# The script used to control virtual interfaces.  This can be overridden on a
+# per-vif basis when creating a domain or a configuring a new vif.  The
+# vif-bridge script is designed for use with the network-bridge script, or
+# similar configurations.
+#
+# If you have overridden the bridge name using
+# (network-script 'network-bridge bridge=<name>') then you may wish to do the
+# same here.  The bridge name can also be set when creating a domain or
+# configuring a new vif, but a value specified here would act as a default.
+#
+# If you are using only one bridge, the vif-bridge script will discover that,
+# so there is no need to specify it explicitly.
+#
+(vif-script vif-bridge)
+
+
+## Use the following if network traffic is routed, as an alternative to the
+# settings for bridged networking given above.
+#(network-script network-route)
+#(vif-script     vif-route)
+
+
+## Use the following if network traffic is routed with NAT, as an alternative
+# to the settings for bridged networking given above.
+#(network-script network-nat)
+#(vif-script     vif-nat)
+
 
 # Dom0 will balloon out when needed to free memory for domU.
 # dom0-min-mem is the lowest memory level (in MB) dom0 will get down to.